package com.qztc.controller;

import com.qztc.hire_end_dto.dto.Brand;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static com.qztc.config.GlobalConfig.SERVICE_URL;

@RestController
@RequestMapping("/consumer/brand")
@Tag(name = "消费者端汉服品牌管理接口")
public class BrandControllerByRestTemplate {
    @Autowired
    private RestTemplate restTemplate;

    @Operation(summary = "查询所有品牌")
    @GetMapping("/getAll")
    public String getAll() {
        return restTemplate.getForObject(SERVICE_URL+"/brand/getAll", String.class);
    }

    @Operation(summary = "获取具体品牌")
    @GetMapping("/getbybrandId")
    public String getbybrandId(@RequestParam Integer brandId) {
        return restTemplate.getForObject(SERVICE_URL+"/brand/getbybrandId?brandId="+brandId, String.class);
    }

    @Operation(summary = "删除品牌")
    @DeleteMapping("/delete")
    public String delete(@RequestBody Brand brand) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Brand> httpEntity = new HttpEntity<>(brand, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/brand/delete", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "修改品牌")
    @PutMapping("/update")
    public String update(@RequestBody Brand brand) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Brand> httpEntity = new HttpEntity<>(brand, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/brand/update", HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "增加品牌")
    @PostMapping("/add")
    public String add(@RequestBody Brand brand) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Brand> httpEntity = new HttpEntity<>(brand, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/brand/add", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "批量删除品牌")
    @DeleteMapping("/deleteMany")
    public String deleteMany(@RequestBody List<Integer> brandIds) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Integer>> httpEntity = new HttpEntity<>(brandIds, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/brand/deleteMany", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "批量添加品牌")
    @PostMapping("/addMany")
    public String addMany(@RequestBody List<Brand> brands) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Brand>> httpEntity = new HttpEntity<>(brands, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/brand/addMany", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }
}
